// SetNormalProfile.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

#include <soundfile.h>
#include <notify.h>
#include <snapi.h>
#include <regext.h>

extern "C" void AudioUpdateFromRegistry();

BOOL PhoneInUse()
{
  DWORD status = 0;

  if (SUCCEEDED(RegistryGetDWORD(SN_PHONEINCOMINGCALL_ROOT, SN_PHONEINCOMINGCALL_PATH, SN_PHONEINCOMINGCALL_VALUE, &status)))
  {
    if (status & SN_PHONEINCOMINGCALL_BITMASK) return TRUE;
  }

  status = 0;

  if (SUCCEEDED(RegistryGetDWORD(SN_PHONECALLCALLING_ROOT, SN_PHONECALLCALLING_PATH, SN_PHONECALLCALLING_VALUE, &status)))
  {
    if (status & SN_PHONECALLCALLING_BITMASK) return TRUE;
  }

  status = 0;

  if (SUCCEEDED(RegistryGetDWORD(SN_PHONECALLTALKING_ROOT, SN_PHONECALLTALKING_PATH, SN_PHONECALLTALKING_VALUE, &status)))
  {
    if (status & SN_PHONECALLTALKING_BITMASK) return TRUE;
  }

  return FALSE;
}

int _tmain(int argc, _TCHAR* argv[])
{
  BOOL loop = TRUE;
  BOOL doBetweenSleep = FALSE;

  while (loop)
  {
    doBetweenSleep = FALSE;

    while (PhoneInUse())
    {
      ::Sleep(5000);
      doBetweenSleep = TRUE;
    }

    if (doBetweenSleep)
    {
      ::Sleep(2500);
    }

    if (!PhoneInUse())
    {
      SNDFILEINFO soundFileInfo;
      soundFileInfo.sstType = SND_SOUNDTYPE_ON;

      if (SUCCEEDED(SndSetSound(SND_EVENT_ALL, &soundFileInfo, TRUE)))
      {
        // This may or may not be required, but does not hurt
        AudioUpdateFromRegistry();

        // Check the profile to see if it switched.
        soundFileInfo.sstType = SND_SOUNDTYPE_DISABLE;
        SndGetSound(SND_EVENT_ALL, &soundFileInfo);

        if (soundFileInfo.sstType == SND_SOUNDTYPE_ON)
        {
          loop = FALSE;
        }
        else
        {
          ::Sleep(5000);
        }
      }
    }
  }

  return 0;
}

